package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UriUtils;
import com.bumptech.glide.Glide;
import com.stark.picselect.entity.SelectMediaEntity;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.MyAlbumBean;
import flc.ast.bean.MyPicLibraryBean;
import flc.ast.databinding.ActivityPicDetailBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class PicDetailActivity extends BaseAc<ActivityPicDetailBinding> {

    public static String imgPath;
    public static boolean isAlbum;
    private boolean isCollect = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_detail;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivPicDetailBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivPicDetailCollect.setOnClickListener(this);
        mDataBinding.tvPicDetailCollect.setOnClickListener(this);
        mDataBinding.tvPicDetailShare.setOnClickListener(this);
        mDataBinding.tvPicDetailPrivacy.setOnClickListener(this);
        mDataBinding.tvPicDetailDelete.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPicDetailCollect:
            case R.id.tvPicDetailCollect://收藏
                List<String> listSp = AppSpUtils.getPicCollectList();
                if (isCollect) {
                    listSp.remove(imgPath);
                    AppSpUtils.savePicCollectList(listSp);
                    isCollect = false;
                } else {
                    if (listSp != null && listSp.size() > 0) {
                        listSp.add(imgPath);
                        AppSpUtils.savePicCollectList(listSp);
                    } else {
                        List<String> listSave = new ArrayList<>();
                        listSave.add(imgPath);
                        AppSpUtils.savePicCollectList(listSave);
                    }
                    isCollect = true;
                }
                isCollect();
                break;
            case R.id.tvPicDetailShare://分享
                IntentUtil.shareImage(mContext, "", imgPath);
                break;
            case R.id.tvPicDetailPrivacy://加入加密
                String filePath = FileUtil.generateFilePath(Content.myPrivacyFile, "." + FileUtils.getFileExtension(imgPath));
                FileUtils.copy(imgPath, filePath);
                ToastUtils.showShort(getString(R.string.add_suc));
                break;
            case R.id.tvPicDetailDelete://删除
                if (isAlbum) {
                    deleteAlbum();
                } else {
                    getPermission();
                }
                break;
        }
    }

    @Override
    protected void initData() {
        List<String> listSp = AppSpUtils.getPicCollectList();
        if (listSp != null && listSp.size() > 0) {
            for (String s : listSp) {
                if (imgPath.equals(s)) {
                    isCollect = true;
                    break;
                }
            }
        }
        Glide.with(mContext).load(imgPath).into(mDataBinding.ivPicDetailImg);
        isCollect();
    }

    private void isCollect() {
        if (isCollect) {
            mDataBinding.ivPicDetailCollect.setImageResource(R.drawable.aquxiaoshouc);
            mDataBinding.tvPicDetailCollect.setText(getString(R.string.cancel_collect));
        } else {
            mDataBinding.ivPicDetailCollect.setImageResource(R.drawable.ashouc);
            mDataBinding.tvPicDetailCollect.setText(getString(R.string.collect_text));
        }
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.reqManageExternalStoragePermission()
                .reqPermissionDesc(getString(R.string.get_all_storage_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        delete();
                    }
                })
                .request();
    }

    private void delete() {
        showDialog(getString(R.string.delete_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        //从此文件夹删除
                        FileUtils.delete(imgPath);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.delete_suc));
                        finish();
                    }
                });
            }
        }, 1000);
    }

    //删除相册图片
    private void deleteAlbum() {
        List<MyPicLibraryBean> listSp = AppSpUtils.getPictureList();
        if (listSp != null && listSp.size() > 0) {
            for (MyPicLibraryBean bean : listSp) {
                if (bean.getId().equals(PicAlbumActivity.albumId)) {
                    bean.getList().remove(imgPath);
                    break;
                }
            }
            AppSpUtils.savePictureList(listSp);
        }
        finish();
    }


}